package roledao

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gconv"
	"xr-game-server/core/cache"
	"xr-game-server/core/constants/common"
	"xr-game-server/core/str"
	"xr-game-server/dto/roledto"
	"xr-game-server/entity"
)

var accountCache *cache.CacheMgr

var roleCache *cache.CacheMgr

func InitRoleDao() {
	accountCache = cache.NewCacheMgr()
	roleCache = cache.NewCacheMgr()

}

// GetRoleBy 根据账号id拉取数据
func GetRoleBy(accountId uint64) []*entity.PlayerRole {
	//命中不了缓存，从数据库拉取数据
	cacheData := accountCache.GetData(accountId, func(ctx context.Context) (value interface{}, err error) {
		//从数据库拉取数据
		roles := make([]*entity.PlayerRole, common.Zero)
		err = g.Model(string(entity.TbPlayerRole)).Unscoped().Where(string(entity.PlayerRoleAccountId), accountId).Scan(&roles)
		return roles, nil
	})
	return cacheData.([]*entity.PlayerRole)
}

func AddRole(role *entity.PlayerRole) {
	roles := GetRoleBy(role.AccountId)
	newRoles := append(roles, role)
	accountCache.FlushCache(role.AccountId, newRoles)
	roleCache.FlushCache(role.ID, role)
}

// GetRoleByRole 根据角色id拉取数据
func GetRoleByRole(roleId uint64) *entity.PlayerRole {
	//命中不了缓存，从数据库拉取数据
	cacheData := roleCache.GetData(roleId, func(ctx context.Context) (value interface{}, err error) {
		//从数据库拉取数据
		var account *entity.PlayerRole
		err = g.Model(string(entity.TbPlayerRole)).Unscoped().WherePri(roleId).Scan(&account)
		if account != nil {
			return account, nil
		} else {
			return entity.NewNullRole(), nil
		}
	})
	return cacheData.(*entity.PlayerRole)
}

func PageRole(req *roledto.PageRoleReq) (total int, ret []*roledto.PageRoleDto) {
	ret = make([]*roledto.PageRoleDto, common.Zero)
	sql := "select  pr.*,sv.*,pr.id role_id,ast.*,pr.created_at created_at  from player_roles pr " +
		"left join accounts ast on ast.id=pr.account_id " +
		" left join" +
		" (select a.*,a.name server_name,z.name zone_name from  servers a left join zones z on z.id=a.zone_id ) sv " +
		" on sv.id=pr.server_id" +
		" where 1=1 "
	param := make([]any, common.Zero)
	if req.RoleId != nil {
		sql += " and pr.id= ? "
		param = append(param, req.RoleId)
	}
	if req.ServerId != nil {
		sql += " and pr.server_id= ? "
		param = append(param, req.ServerId)
	}
	if req.StartTime != nil && req.EndTime != nil {
		sql += " and pr.created_at between ? and ? "
		param = append(param, req.StartTime, req.EndTime)
	}
	sql += " order by pr.created_at desc limit ?,?"
	ctx := gctx.New()
	if len(param) == common.Zero {
		total, _ = g.DB().GetCount(ctx, str.GetCountSQL(sql))
	} else {
		total, _ = g.DB().GetCount(ctx, str.GetCountSQL(sql), param)
	}
	if total == common.Zero {
		return total, ret
	}
	param = append(param, req.PageIndex, req.PageSize)
	list, _ := g.DB().GetAll(ctx, sql, param)
	if nil == list {
		return total, ret
	}
	for _, item := range list {
		retItem := roledto.NewPageRoleDto()
		gconv.Struct(item, retItem)
		retItem.AddTimeSpecial()
		ret = append(ret, retItem)
	}
	return total, ret
}
